<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/heima.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <!-- 页面主体区域 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!-- 侧边栏菜单区 -->
                <!-- router :开启路由模式，默认跳转路由地址就是index值 -->
                <el-menu background-color="#545c64" text-color="#fff" active-text-color="#409eff" :unique-opened="true"
                    :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
                    <!-- 一级菜单 -->
                    <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
                        <!-- 一级菜单的模板区域 -->
                        <template slot="title">
                            <!-- 图标 -->
                            <i :class="iconsObj[item.id]"></i>
                            <!-- 文本 -->
                            <span>{{ item.authName }}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id">
                            <template slot="title">
                                <!-- 图标 -->
                                <i class="el-icon-menu"></i>
                                <!-- 文本 -->
                                <span @click="saveStatus('/' + subItem.path)">{{ subItem.authName }}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右侧内容主体 -->
            <el-main>
                <!-- 路由占位符 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    data() {
        return {
            isCollapse: false, // 是否折叠
            activePath: '', // 当前激活菜单项
            menuList: [],
            iconsObj: {
                '125': 'iconfont icon-user',
                '103': 'iconfont icon-tijikongjian',
                '101': 'iconfont icon-shangpin',
                '102': 'iconfont icon-danju',
                '145': 'iconfont icon-baobiao'
            }
        }
    },
    created() {
        this.getMenuList()
        let activePath = window.sessionStorage.getItem('active')
        if (activePath != null)
            this.activePath = activePath
    },
    methods: {
        logout() {
            window.sessionStorage.clear() // 清空token
            this.$router.push('/login') // 重定向到登录界面
        },
        // 获取所有菜单
        async getMenuList() {
            const res = await this.$http.get('menus')
            console.log(res.data)
            if (res.meta.status !== 200) {
                return this.$message.error(res.meta.msg)
            }
            this.menuList = res.data
            console.log(res);
        },
        // 点击侧边栏收起事件
        toggleCollapse() {
            this.isCollapse = !this.isCollapse
        },
        // 菜单点击事件
        saveStatus(path) {
            window.sessionStorage.setItem('active', path)
            this.activePath = path
        }
    }
}
</script>

<style lang="less" scoped>
.home-container {
    height: 100%;
}

.el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between; // 左右贴边对齐
    padding-left: 0; // 消除左贴边的间隔
    align-items: center; // 使得按钮上下居中
    color: #fff; // 文本颜色
    font-size: 20px; // 文本大小

    // 布局嵌套
    >div {
        display: flex;
        align-items: center; // 纵向居中

        span {
            margin-left: 15px; // 使文本和图片间隔开来
        }
    }
}

.el-aside {
    background-color: #333744;

    .el-menu {
        border-right: 0;
    }
}

.el-main {
    background-color: #eaeaea;
}

.iconfont {
    padding-right: 10px;
}

.toggle-button {
    background-color: #4a5064;
    font-size: 12px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>